package top.continew.minithreadpool.ui.cyberpunkUi;

import javax.swing.*;
import java.awt.*;

public class CyberButton extends JButton {
    private final Color glowColor;

    public CyberButton(String text, Color glowColor) {
        super(text);
        this.glowColor = glowColor;
        setContentAreaFilled(false);
        setBorderPainted(false);
        setFocusPainted(false);
        setFont(new Font("OCR A Extended", Font.BOLD, 14));
        setForeground(Color.WHITE);
        setPreferredSize(new Dimension(150, 40));
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制按钮背景
        g2d.setColor(new Color(30, 30, 50));
        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);

        // 绘制发光边框
        g2d.setColor(glowColor);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawRoundRect(1, 1, getWidth()-2, getHeight()-2, 20, 20);

        // 绘制发光效果
        for (int i = 0; i < 5; i++) {
            g2d.setColor(new Color(
                    glowColor.getRed(),
                    glowColor.getGreen(),
                    glowColor.getBlue(),
                    50 - i*10
            ));
            g2d.drawRoundRect(
                    1+i, 1+i,
                    getWidth()-2-i*2, getHeight()-2-i*2,
                    20, 20
            );
        }

        // 绘制文字
        g2d.setColor(Color.WHITE);
        FontMetrics fm = g2d.getFontMetrics();
        int x = (getWidth() - fm.stringWidth(getText())) / 2;
        int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
        g2d.drawString(getText(), x, y);

        // 鼠标悬停效果
        if (getModel().isRollover()) {
            g2d.setColor(new Color(255, 255, 255, 30));
            g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
        }
    }
}
